package com.aspect;

import com.advice.AdviceTarget;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


/**
 * 用注解实现切面进行增强
 */

@Aspect
//  声明这是一个切面
public class AspectAdvice {
    @Pointcut ("execution(* com.advice.AdviceTarget.*(..))") //AdviceTarget类下所有方法都会被切掉切入
    public void myPoint() {
        System.out.println("切点");
    }

    @Before("myPoint()")
    public void before(JoinPoint joinPoint) {
        System.out.println("前置增强start");
        System.out.println("连接点对象:"+joinPoint.getTarget().getClass());
        System.out.println("连接点方法:"+joinPoint.getSignature().getName());
        System.out.println("连接点参数:"+joinPoint.getArgs());
        if(joinPoint.getSignature().getName().equals("testAdvice")) {
            System.out.println("testAdvice ++++1000");
        }
        System.out.println("前置增强end");
    }
    @AfterThrowing("myPoint()")
    public void afterThrowing(JoinPoint joinPoint) throws Throwable {
        System.out.println("异常增强start");
        System.out.println("连接点对象" + joinPoint.getTarget().getClass());
        System.out.println("连接点方法:" + joinPoint.getSignature().getName());
        if(joinPoint.getTarget().getClass().equals(com.advice.AdviceTarget.class)) {
            if (joinPoint.getSignature().getName().equals("testException")) {
                System.out.println("快把数据库连接池关了~~~~");
            }
        }
        System.out.println("异常增强end");
    }

}
